<?php
/**
 * alltosun.com 积分记录控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2014-11-17 下午6:41:46 $
 * $Id: index.php 160060 2016-01-06 06:32:31Z gaoa $
*/

class Action
{
    private $per_page = 20;
    
    
    
    /**
     * 为用户添加积分
     */
    public function add_score()
    {
        $id = Request::getParam('id', 0);
        
        if (!$id) {
        	return false;
        }
        
        // 获取用户信息
        $user_info = _uri('user', $id);
        if (!$user_info) {
        	return '用户不存在!';
        }
        
        $filter = array();
        $parent_type_list = _model('score_type')->getList(array('parent_id'=>0, 'status'=>1));
    
        $parent_ids      = array();
        $score_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {
    
                array_push($parent_ids, $value['id']);
                $score_type_list[$key]['id'] = $value['id'];
    
                $score_type_list[$key]['res_name'] = $value['res_name'];
                $score_type_list[$key]['type_list'] = _model('score_type')->getList(array('parent_id'=>$value['id'], 'status'=>1));
    
            }
        }
    
        Response::assign('user_info', $user_info);
        Response::assign('score_type_list', $score_type_list);
        Response::display('admin/add_score.html');
    }
    
    
    
    /**
    * 为用户添加积分
    */
    public function add()
    {
        $filter = array();
        $parent_type_list = _model('score_type')->getList(array('parent_id'=>0, 'status'=>1));

        $parent_ids      = array();
        $score_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {

                array_push($parent_ids, $value['id']);
                $score_type_list[$key]['id'] = $value['id'];

                $score_type_list[$key]['res_name'] = $value['res_name'];
                $score_type_list[$key]['type_list'] = _model('score_type')->getList(array('parent_id'=>$value['id'], 'status'=>1));

            }
        }


        Response::assign('score_type_list', $score_type_list);
        Response::display('admin/add.html');
    }

    /**
    * 添加
    */
    public function create()
    {
        $score_info = Request::getParam('score', array());
        
        $user_id = $score_info['user_id'];
        //获取当前登陆的用户ID
        $admin_id = user_helper::get_admin_id();

        $score = array();
        $score['user_id']  = $user_id;
        $score['admin_id'] = $admin_id;
        $score['score']    = $score_info['score'];
        $score['add_time'] = date('Y-m-d H:i:s', time());
        $score['res_id']   = $score_info['res_id'];

        $id = _model('score_record')->create($score);
        //增加用户的总积分
        $user_info = _uri('user', $user_id);
        
        if ($user_info) {
        	$new_score = $score['score'] + $user_info['score'];
        	_model('user')->update($user_id, array('score'=>$new_score));
        } else {
            return '未找到此用户信息!';
        }
        
//         // 因为是后台添加使用默认的值插入到(进货和发货)
//         if ($id && ($score['res_id'] == 2 || $score['res_id'] == 3) ) {
//             _model('score_orders')->create(array('score_id'=>$id));
//         }
        
        // 添加积分完成后执行推送
        //处理消息内容
	    $msg = array();
	    $msg['content']  = message_center_helper::get_push_template('score', array('score'=>$score['score']))['title'];
	    $msg['res_id']   = $id;
	    // 根据res_id获取积分类型
	    $type_info = _model('score_type')->getFields('res_name', array('id'=>$score['res_id']));
	    $msg['res_name'] = $type_info[0];
	    
	    // 获取用户的角色
	    $user_role_id = user_helper::get_user_role_id($user_id);
	    $msg['role_id'] = $user_role_id;
	    $result = '';
	    if ($user_role_id == 5 || $user_role_id == 10) {
	    	$result = _widget('push')->send($msg, array($user_id), 'manage');
	    } else {
	        $result = _widget('push')->send($msg, array($user_id), 'store');
	    }
	    
	    // 将积分消息填入到消息中心表和消息用户关系表中
	    message_center_helper::add_message($msg['content'], 'score', $user_id);
	    
        Response::assign('status', 1);
        Response::redirect('score_record/admin');

    }
    
    
/*     public function my_rule()
    {
        $record_list = _model('bbcoin_record')->getList(array('`id` > '=>0));
        foreach ($record_list as $k=>$v){
            $role_id = user_helper::get_user_role_id($v['user_id']);
            _model('bbcoin_record')->update(array('`id` > '=>0), array('role_id'=>$role_id));
        }
        echo 'hahahah!!!!';
    } */


    /**
    * 积分记录列表
    */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::Get('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        //过滤条件
        $filter = array();

        // 用户名的过滤
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $ids = user_helper::get_user_ids_by_name($search_filter['user_name']);
            $filter['user_id'] = $ids;
        }
        // 操作人的过滤
        if (isset($search_filter['admin_name']) && $search_filter['admin_name']) {
            $ids = user_helper::get_user_ids_by_name($search_filter['admin_name']);
            $filter['admin_id'] = $ids;
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        if (!$filter) $filter['user_id >'] = 0;

        $table_name = 'score_record';
        $score_list = get_data_list($table_name, $filter, ' ORDER BY `add_time` DESC ', $page, $this->per_page);
        if ($score_list) {

            $score_list = score_record_helper::score_record_format($score_list);
            // 分配用户名和操作人姓名
            foreach ($score_list as $k => $v) {
                $score_list[$k]['user_name'] = user_helper::display_name($v['user_id']);
            }
        }


        Response::assign('score_list', $score_list);
        Response::assign('status', $status);

        Response::assign('is_advenced', $is_advenced);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/score_list.html');
    }


    /**
    * 伪删除
    */
    public function delete()
    {
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('score_record')->update($v, array('status' => 0));
        }

        return 'ok';
    }

    /**
    * 恢复
    */
    public function change_status()
    {
        //接收id
        $id = Request::Get('id', 1);

        _model('score_record')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('score_record/admin?status=1'));
    }

    /**
    * 物理删除
    */
    public function delete_record()
    {
        //接收id
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('score_record')->delete($v);
        }

        return 'ok';
    }

    /**
    * 接受ajax传递的用户名,进行查询
    */
    public function ajax_search()
    {
        $user_name = Request::getParam('user_name', '');

        if (!$user_name) {
            echo 0;
        }
        //根据接受的值查询符合条件的用户
        $user_ids = user_helper::get_user_ids_by_name($user_name);

        $user_list = array();
        foreach ($user_ids as $k => $v) {
            array_push($user_list, user_helper::get_user_info($v));
        }
        if ($user_list) {
            echo json_encode($user_list);
        } else {
            echo 0;
        }

    }

    /**
    * 接受ajax传递的用户id,获取对应旗下的分店列表
    */
    public function ajax_search_store_list()
    {
        $user_id = Request::getParam('user_id', 1);

        if (!$user_id) {
            echo 0;
        }

        $store_list = array();

        // 根据用户id获取旗下的商铺列表
        $store_list = store_helper::get_store($user_id);

        if ($store_list) {
            echo json_encode($store_list);
        } else {
            echo 0;
        }

    }
}

?>